package com.ztf0337.mycurriculumdesign.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.entity.ParkLot;
import com.ztf0337.mycurriculumdesign.service.OrderService;
import com.ztf0337.mycurriculumdesign.service.ParkLotService;

import java.io.File;

public class ParkLotDetailActivity extends AppCompatActivity {

    private ParkLotService parkLotService;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_park_lot_detail);

        parkLotService = new ParkLotService(this);


        Intent intent = getIntent();
        Long id = intent.getLongExtra("id", -1L);
        ParkLot parkLot = parkLotService.getParkLotById(id);
        if (parkLot == null) {
            Toast.makeText(this, "系统错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        ImageView btnBack,img;
        TextView tvParkName,tvBusinessStatus,tvParkLotType,tvParkNumber,tvParkAddress,tvInformation,tvParkPhone;

        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> {
            finish();
        });

        img = findViewById(R.id.imageView);
        File filesDir = this.getFilesDir();
        File imagesDir = new File(filesDir, "gate_images");
        String fileName = parkLot.getUrl();
        Log.d("文件测试", "获取到图片"+fileName);
        File targetImage = new File(imagesDir, fileName);
        if (targetImage.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(targetImage.getAbsolutePath());
            img.setImageBitmap(bitmap);
        } else {
            img.setImageResource(R.drawable.img_default_park_lot);
        }

        tvParkName = findViewById(R.id.tvParkName);
        tvParkName.setText(parkLot.getParkLotName());
        tvBusinessStatus = findViewById(R.id.tvBusinessStatus);
        if (parkLot.getStatus()) {
            tvBusinessStatus.setTextColor(Color.GREEN);
        } else {
            tvBusinessStatus.setTextColor(Color.RED);
        }
        tvBusinessStatus.setText(parkLot.getStatus() ? "营业中" : "停用中");
        tvParkLotType = findViewById(R.id.tvParkLotType);
        tvParkLotType.setText(parkLot.getType());
        tvParkNumber = findViewById(R.id.tvParkNumber);
        tvParkNumber.setText(parkLot.getAvailableSpaces() +"/" +parkLot.getTotalSpaces());
        tvParkAddress = findViewById(R.id.tvParkAddress);
        tvParkAddress.setText(parkLot.getLocal());
        tvInformation = findViewById(R.id.tvInformation);
        tvInformation.setText(parkLot.getInformation());
        tvParkPhone = findViewById(R.id.tvParkPhone);
        tvParkPhone.setText(parkLot.getPhone());


    }
}
